package homework;

public class CreditCard extends DepositCard{
    private double maxOverdraft;
    private double overdrawn;

    public CreditCard(String account, double balance,double maxOverdraft) {
        super(account, balance);
        this.maxOverdraft = maxOverdraft;
    }

    @Override
    public void withdraw(double money) {
        if (money < 0) {
            System.out.println("取款金额不能为负数");
            return;
        }

        double availableBalance = super.getBalance() + (maxOverdraft - overdrawn);
        if (money > availableBalance) {
            System.out.println("超过可透支额度");
            return;
        }

        if (money <= super.getBalance()) {
            super.withdraw(money);
            overdrawn = 0;
        } else {
            double overdraftAmount = money - super.getBalance();
            super.withdraw(super.getBalance());
            overdrawn += overdraftAmount;
            System.out.println("透支成功，当前透支金额：" + overdrawn);
        }
    }

    @Override
    public void save(double money) {
        if (money < 0) {
            System.out.println("存款金额不能为负数");
            return;
        }
        if (money <= overdrawn) {
            overdrawn -= money;
            System.out.println("存款成功，已偿还部分透支金额，当前透支金额：" + overdrawn);
        } else {
            super.save(money - overdrawn);
            overdrawn = 0;
            System.out.println("存款成功，已偿还所有透支金额，当前余额：" + super.getBalance());
        }
    }
}
